package com.student.web.admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * get/post 参数cookie
 * 删除cookie session
 * @author lin945
 * @date 2020/5/5 21:09
 */
@WebServlet("/logout")
public class AdminLogOut extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Cookie[] cookies  = req.getCookies();
        if (session.getAttribute("username") == null) {//防止非法入侵
            resp.sendRedirect(req.getContextPath() + "/login");
            return;
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("admin")){
                cookie.setMaxAge(0);
                resp.addCookie(cookie);
            }
        }
        session.removeAttribute("username");//移除session域的管理员名字
        String path = req.getContextPath();
        resp.sendRedirect(path+"/login");//重定向回登录页面

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
